

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xyz.FilmContainer;

/**
 * Servlet implementation class LoadFilm
 */
@WebServlet("/jsp/films/*")
public class LoadFilm extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoadFilm() {
        super();
        
    }
    
    static FilmContainer fc=new FilmContainer();
	
	public static FilmContainer getFc() {
		return fc;
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//PrintWriter pw=response.getWriter();
		
		//String path=request.getServletPath();
		String path=request.getPathTranslated();
		String filmName="";
		int i=path.length()-1;
		while(i>=0 && path.charAt(i)!=92){
			filmName+=path.charAt(i--);
		}
		//pw.println(filmName);
		i=0;
		filmName=new StringBuilder(filmName).reverse().toString();
		String buf="";
		while(i+3<filmName.length() && (filmName.charAt(i)!='.' || filmName.charAt(i+1)!='j' || filmName.charAt(i+2)!='s' || filmName.charAt(i+3)!='p')){
			i++;
		}
		for (int j=0; j<i; j++){
			buf+=filmName.charAt(j);
		}
		filmName=buf;
		
		
		
		boolean f=false;
		for (i=0; i<fc.getFilms().size(); i++){
			if (fc.getFilms().get(i).getId().equals(filmName)){
				f=true;
				break;
			}
		}
		
		if (f){
			request.setAttribute("thisFilm", fc.getFilms().get(i));
			request.setCharacterEncoding("UTF-8");
			RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/jsp/chosenFilm.jsp");
			view.forward(request, response);
		}
		
		//pw.println(path);
		//pw.println(filmName);
		//pw.print(Boolean.valueOf(f).toString());
		
		//pw.close();
				
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
